package com.prj.ufdm.demo.tutorial.despatterns.structural.proxy.dynamicproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyCalculator {

	private CalculatorService target;
	
	ProxyCalculator(CalculatorService target){
		this.target = target;
	}
	
	public CalculatorService getCalculatorServiceProxy() {
		ClassLoader loader = target.getClass().getClassLoader();
		Class [] interfaces = new Class[] {CalculatorService.class};
		InvocationHandler h = new InvocationHandler() {
			@Override
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
				System.out.println(method.getName());
				method.invoke(target, args);
				return 0;
			}
		};
		return (CalculatorService) Proxy.newProxyInstance(loader, interfaces, h);
	}
}
